package com.topisv.tms.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.dom4j.Element;
import org.springframework.web.util.HtmlUtils;

import com.topisv.tms.entity.stock.instorage.InstorageUpSubTaskItem;
import com.topisv.tms.service.vo.PrinterModel;
import com.topisv.tms.utils.io.ReaderXML;

public abstract class PrintModelUtil {

	public static String getFileNameExpress() {
		return UUID.randomUUID().toString() + ".express";
	}
	
	public static String getFileNameStyle() {
		return UUID.randomUUID().toString() + ".style";
	}

	/**
	 * 对变量进行替换
	 * 
	 * @param content
	 * @param printerModer
	 * @return
	 * @throws Exception
	 */
	public static String replaceParams(String content, PrinterModel printerModer)
			throws Exception {
		for (Class<?> clazz = printerModer.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
			Field[] fields = clazz.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				String paramName = fields[i].getName();
				Method method = clazz.getMethod(getMethod(paramName));
				Object object = method.invoke(printerModer);
				if (object != null) {
					content = content.replace("${" + paramName + "}", object.toString());
				} else {
					content = content.replace("${" + paramName + "}", "");

				}
			}
		}
		return content;
	}

	private static String getMethod(String param) {
		StringBuffer sb = new StringBuffer(param.subSequence(0, 1));
		String result = "get" + sb.toString().toUpperCase()
				+ param.substring(1);
		return result;
	}
	
	/**
	 *构造上架任务打印内容
	 * @param items
	 * @return List<String>
	 */
	public static String buildUpTaskPrintContent(List<InstorageUpSubTaskItem> items){
		Element root = ReaderXML.read("/template/upTask.xml");
		Element tableElement = root.element("html").element("body").element("table").element("tr").element("td").element("table");
		Element theadElement = tableElement.element("thead");
		//处理1:
		Element tdElement = theadElement.element("tr").element("td");
		tdElement.setText(MessageFormat.format(tdElement.getText(), items.get(0).getSubTaskCode()));
		//处理2：
		Element trElement = tableElement.element("tbody").element("tr");
		String contentText = trElement.asXML();
		StringBuilder sb = new StringBuilder();
		for (InstorageUpSubTaskItem item : items) {
			sb.append(MessageFormat.format(contentText, item.getSkuCode(),item.getBoxCode(),item.getStockCode(),item.getNum()));
		}
		Element tbodyElement = trElement.getParent();
		tbodyElement.remove(trElement);
		tbodyElement.setText(sb.toString());
		return HtmlUtils.htmlUnescape(root.element("html").asXML());
	}
	
	public static void main(String[] args) throws Exception {
		PrinterModel model = new PrinterModel();
		model.setFromAddress("sddddd");
	}
	
	/**
	 *构造LPN上架任务打印内容
	 * @param items
	 * @return List<String>
	 */
	public static String buildUpLpnTaskPrintContent(String lpn,List<Map<String,Object>> list){
		Element root = ReaderXML.read("/template/lpnUpTask.xml");
		Element tableElement = root.element("html").element("body").element("table").element("tr").element("td").element("table");
		Element theadElement = tableElement.element("thead");
		//处理1:
		Element tdElement = theadElement.element("tr").element("td");
		tdElement.setText(MessageFormat.format(tdElement.getText(), lpn));
		//处理2：
		Element trElement = tableElement.element("tbody").element("tr");
		String contentText = trElement.asXML();
		StringBuilder sb = new StringBuilder();
		for (Map<String,Object> map : list) {
			sb.append(MessageFormat.format(contentText, map.get("sku"),null,null,map.get("unUpNum")));
		}
		Element tbodyElement = trElement.getParent();
		tbodyElement.remove(trElement);
		tbodyElement.setText(sb.toString());
		return HtmlUtils.htmlUnescape(root.element("html").asXML());
	}
}
